package ams.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@SuppressWarnings("unchecked")
@Repository
public class BaseDao<T> implements IBaseDao<T> {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public T get(Class<T> c, Serializable id) {
		this.getCurrentSession().get(c, id);
		return null;
	}

	@Override
	public void save(T o) {
		this.getCurrentSession().save(o);

	}

	@Override
	public void delete(T o) {
		this.getCurrentSession().delete(o);
	}

	@Override
	public void update(T o) {
		this.getCurrentSession().update(o);

	}

	@Override
	public void saveOrUpdate(T o) {
		this.getCurrentSession().saveOrUpdate(o);

	}

	@Override
	public T get(String hql) {
		List<T> list = this.Find(hql);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public T get(String hql, Object[] paramArray) {
		List<T> list = this.Find(hql, paramArray);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public T get(String hql, List<Object> paramList) {
		List<T> list = this.Find(hql, paramList);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<T> Find(String hql) {
		return this.getCurrentSession().createQuery(hql).list();
	}

	@Override
	public List<T> Find(String hql, Integer page, Integer rows) {
		return this.getCurrentSession().createQuery(hql).setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@Override
	public List<T> Find(String hql, Object[] paramArray) {
		Query query = this.getCurrentSession().createQuery(hql);
		if (paramArray != null && paramArray.length > 0) {
			for (int i = 0; i < paramArray.length; i++) {
				query.setParameter(i, paramArray[i]);
			}
		}
		return query.list();
	}

	@Override
	public List<T> Find(String hql, Object[] paramArray, Integer page, Integer rows) {
		Query query = this.getCurrentSession().createQuery(hql);
		if (paramArray != null && paramArray.length > 0) {
			for (int i = 0; i < paramArray.length; i++) {
				query.setParameter(i, paramArray[i]);
			}
		}
		return query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@Override
	public List<T> Find(String hql, List<Object> paramList) {
		Query query = this.getCurrentSession().createQuery(hql);
		if (paramList != null && paramList.size() > 0) {
			for (int i = 0; i < paramList.size(); i++) {
				query.setParameter(i, paramList.get(i));
			}
		}
		return query.list();
	}

	@Override
	public List<T> Find(String hql, List<Object> paramList, Integer page, Integer rows) {
		Query query = this.getCurrentSession().createQuery(hql);
		if (paramList != null && paramList.size() > 0) {
			for (int i = 0; i < paramList.size(); i++) {
				query.setParameter(i, paramList.get(i));
			}
		}
		return query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}
	
	@Override
	public List<T> Find(String hql, Map<String, Object> paramMap) {
		Query query = this.getCurrentSession().createQuery(hql);
		for (String paramName : paramMap.keySet()) {
			Object param = paramMap.get(paramName);
			if (param instanceof Object[]) {
				query.setParameterList(paramName, (Object[]) param);
			} else if (param instanceof Collection<?>) {
				query.setParameterList(paramName, (Collection<?>) param);
			} else {
				query.setParameter(paramName, param);
			}
		}
		return query.list();
	}

	@Override
	public Long count(String hql) {
		return (Long) this.getCurrentSession().createQuery(hql).uniqueResult();
	}

	@Override
	public Long count(String hql, Object[] paramArray) {
		Query query = this.getCurrentSession().createQuery(hql);
		if (paramArray != null && paramArray.length > 0) {
			for (int i = 0; i < paramArray.length; i++) {
				query.setParameter(i, paramArray[i]);
			}
		}
		return (Long) query.uniqueResult();
	}

	@Override
	public Long count(String hql, List<Object> paramList) {
		Query query = this.getCurrentSession().createQuery(hql);
		if (paramList != null && paramList.size() > 0) {
			for (int i = 0; i < paramList.size(); i++) {
				query.setParameter(i, paramList.get(i));
			}
		}
		return (Long) query.uniqueResult();
	}
	
	@Override
	public Long count(String hql, Map<String, Object> paramMap) {
		Query query = this.getCurrentSession().createQuery(hql);
		for (String paramName : paramMap.keySet()) {
			Object param = paramMap.get(paramName);
			if (param instanceof Object[]) {
				query.setParameterList(paramName, (Object[]) param);
			} else if (param instanceof Collection<?>) {
				query.setParameterList(paramName, (Collection<?>) param);
			} else {
				query.setParameter(paramName, param);
			}
		}
		return (Long) query.uniqueResult();
	}

	@Override
	public Integer executeHql(String hql) {
		this.getCurrentSession().createQuery(hql).executeUpdate();
		return null;
	}

	@Override
	public Integer executeHql(String hql, Object[] paramArray) {
		Query query = this.getCurrentSession().createQuery(hql);
		if (paramArray != null && paramArray.length > 0) {
			for (int i = 0; i < paramArray.length; i++) {
				query.setParameter(i, paramArray[i]);
			}
		}
		return query.executeUpdate();
	}

	@Override
	public Integer executeHql(String hql, List<Object> paramList) {
		Query query = this.getCurrentSession().createQuery(hql);
		if (paramList != null && paramList.size() > 0) {
			for (int i = 0; i < paramList.size(); i++) {
				query.setParameter(i, paramList.get(i));
			}
		}
		return query.executeUpdate();
	}

	@Override
	public Integer executeHql(String hql, Map<String, Object> paramMap) {
		Query query = this.getCurrentSession().createQuery(hql);
		for (String paramName : paramMap.keySet()) {
			Object param = paramMap.get(paramName);
			if (param instanceof Object[]) {
				query.setParameterList(paramName, (Object[]) param);
			} else if (param instanceof Collection<?>) {
				query.setParameterList(paramName, (Collection<?>) param);
			} else {
				query.setParameter(paramName, param);
			}
		}
		return query.executeUpdate();
	}

	/*
	  * <p>Title: findBySQL</p>
	  * <p>Description: </p>
	  * @param sql
	  * @param T
	  * @return
	  * @see ams.dao.IBaseDao#findBySQL(java.lang.String, java.lang.Class)
	  */
	
	
	@SuppressWarnings("rawtypes")
	@Override
	public List<T> findBySQL(String sql, Class T) {
		return this.getCurrentSession().createSQLQuery(sql).addEntity(T).list();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<T> findBySQL(String sql, Class T, Object[] paramArray) {
		SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
		if (paramArray != null && paramArray.length > 0) {
			for (int i = 0; i < paramArray.length; i++) {
				query.setParameter(i, paramArray[i]);
			}
		}
		return query.addEntity(T).list();
	}
	
	@Override
	public void executeBySQL(String sql) {
		this.getCurrentSession().createSQLQuery(sql).executeUpdate();
	}

}
